package com.cory.boot.utils;

import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import lombok.SneakyThrows;
import org.springframework.core.task.TaskExecutor;

/**
 * @author xiongcong
 * @date 2022-04-01
 */
public class TaskPoolUtils {

    public static void run(Runnable task) {
        SpringUtils.getBean(TaskExecutor.class).execute(task);
    }

    @SneakyThrows
    public static void runFuture(List<Runnable> tasks) {
        ExecutorService executor = Executors.newCachedThreadPool();

        for (Runnable task : tasks) {
            Future<?> future = executor.submit(task);
            future.get();
        }

    }
}
